﻿using Admin.NET.Core.Service;
using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
using Furion.DatabaseAccessor;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.SemanticSemproxySearchResponse.Types;
using Mapster;

namespace Admin.NET.Application;
/// <summary>
/// 组态设计服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class BoardDisplayDesignService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<BoardDisplayDesign> _rep;
    private readonly BardDesignDetailService _boardDesignDetailService;

    public BoardDisplayDesignService(BardDesignDetailService boardDesignDetailService, SqlSugarRepository<BoardDisplayDesign> rep)
    {
        _rep = rep;
        _boardDesignDetailService = boardDesignDetailService;
    }

    /// <summary>
    /// 分页查询组态设计
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<BoardDisplayDesignOutput>> Page(BoardDisplayDesignInput input)
    {
        var query = _rep.AsQueryable()
            .WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
                u.DesignNo.Contains(input.SearchKey.Trim())
                || u.Name.Contains(input.SearchKey.Trim())
            )
            .WhereIF(!string.IsNullOrWhiteSpace(input.DesignNo), u => u.DesignNo.Contains(input.DesignNo.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name.Trim()))
            .Select<BoardDisplayDesignOutput>();
        return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加组态设计
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task<long> Add(AddBoardDisplayDesignInput input)
    {
        var entity = input.Adapt<BoardDisplayDesign>();
        await _rep.InsertAsync(entity);
        _boardDesignDetailService.UpdateBoardDesignDetail(entity.Id,input.ExtDetail);
        return entity.Id;
    }

    /// <summary>
    /// 删除组态设计
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(DeleteBoardDisplayDesignInput input)
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        //await _rep.FakeDeleteAsync(entity);   //假删除
        await _rep.DeleteAsync(entity);   //真删除
    }

    /// <summary>
    /// 更新组态设计
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(UpdateBoardDisplayDesignInput input)
    {
        var entity = input.Adapt<BoardDisplayDesign>();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
        _boardDesignDetailService.UpdateBoardDesignDetail(entity.Id, input.ExtDetail);
    }

    /// <summary>
    /// 获取组态设计
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<BoardDisplayDesign> Detail([FromQuery] QueryByIdBoardDisplayDesignInput input)
    {
        return await _rep.GetFirstAsync(u => u.Id == input.Id);
    }
    /// <summary>
    /// 获取组态设计
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "getDetailByNo")]
    public async Task<BoardDisplayDesignOutputAll> getDetailByNo(string no)
    {
        var bd= _rep.GetFirstAsync(u => u.DesignNo == no).Result;
        BoardDisplayDesignOutputAll t = new BoardDisplayDesignOutputAll();
        t.main = bd;
        if (bd != null)
        {
            var detail = _boardDesignDetailService.GetDesignExtStepList(bd.Id);
            t.extDetail = detail.Result;
           
        }
        return t;



    }
    /// <summary>
    /// 获取组态设计列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<List<BoardDisplayDesignOutput>> List([FromQuery] BoardDisplayDesignInput input)
    {
        return await _rep.AsQueryable().Select<BoardDisplayDesignOutput>().ToListAsync();
    }





}

